<template>
    <el-dialog v-model="editData.visible" :title="editData.title" center width="36%" top="5vh" :close-on-click-modal="false" append-to-body>
        <div class="dialog-content">
            <el-form :model="editData.form" label-width="auto" style="width:100%" ref="formRef" :rules="editData.rules">
                <el-row :gutter="50">
                    <el-col :xs="24" :sm="24" :md="12" :lg="24" :xl="24">
                        <el-form-item label="文章标题" prop="title">
                            <el-input v-model="editData.form.title" placeholder="请输入文章标题" clearable/>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                        <el-form-item label="文章描述" prop="description">
                            <el-input type="textarea" clearable v-model="editData.form.description" placeholder="请输入文章描述" :rows="5"/>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                        <el-form-item label="封面图片" prop="coverImageUrl">
                            <upload-img v-model="editData.form.coverImageUrl" :aspect-ratio="5/4" mode="single"></upload-img>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="12">
                        <el-form-item label="是否私密" prop="status">
                            <el-switch v-model="editData.form.status" :active-value='1' :inactive-value='0'/>
                        </el-form-item>
                    </el-col>

                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                        <el-form-item label="文章内容" prop="content">
                            <king-editor v-model="editData.form.content" v-if="editData.visible" style="width:100%"></king-editor>
                        </el-form-item>
                    </el-col>

                </el-row>
            </el-form>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="editData.visible = false">取消</el-button>
                <el-button type="primary" @click="submitForm">确定</el-button>
            </div>
        </template>
    </el-dialog>
</template>


<script setup>
import {nextTick, reactive, ref, useTemplateRef} from 'vue'
import {ElMessage} from 'element-plus'
import articleApi from 'api/model/article.js'
import KingEditor from '@/components/king-editor/index-old.vue'
import UploadImg from 'components/comp-upload/upload-img.vue'

const emits = defineEmits(['update'])
defineExpose({openDialog})
const formRef = useTemplateRef('formRef')
const editData = reactive({
    type: 'add',
    visible: false,
    form: {
        id: '',
        title: '',
        description: '',
        status: 1,
        content: '',
    },
    rules: {
        title: [
            {required: true, message: '请输入文章标题', trigger: 'blur'},
            {min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur'}
        ],
    }
})

/**
 * 打开弹框
 * @param type
 * @param item
 */
function openDialog(type, item) {
    editData.title = type === 'add' ? '新增文章' : '编辑文章'
    editData.visible = true
    editData.type = type
    editData.form.id = ''
    nextTick(() => {
        formRef.value.resetFields()
        if (type === 'update') {
            const {id} = item || {}
            editData.form.id = id
            getDetails()
        }
    })
}

/**
 * 提交表单
 */
function submitForm() {
    formRef.value.validate(async valid => {
        if (!valid) return
        editData.type === 'add' ? addItem() : updateItem()
    })
}

/**
 *  新增操作
 */
async function addItem() {
    const {code, data = []} = await articleApi.add.post({
        title: editData.form.title,
        description: editData.form.description,
        content: editData.form.content,
        status: editData.form.status,
        coverImageUrl: editData.form.coverImageUrl,

    })
    if (code === 200) {
        ElMessage.success('新增成功')
        editData.visible = false
        emits('update')
    } else {
        ElMessage.error('新增失败')
    }
}

/**
 *  更新操作
 */
async function updateItem() {
    const {code, data = []} = await articleApi.update.post({
        id: editData.form.id,
        title: editData.form.title,
        description: editData.form.description,
        content: editData.form.content,
        status: editData.form.status,
        coverImageUrl: editData.form.coverImageUrl,
    })
    if (code === 200) {
        ElMessage.success('编辑成功')
        editData.visible = false
        emits('update')
    } else {
        ElMessage.error('编辑失败')
    }
}

/**
 * 获取详情
 */
async function getDetails() {
    const {code, data = []} = await articleApi.details.get({
        id: editData.form.id
    })
    if (code === 200) {
        const {title, description, coverImageUrl, status, content} = data || {}
        editData.form.title = title
        editData.form.description = description
        editData.form.coverImageUrl = coverImageUrl
        editData.form.status = status
        editData.form.content = content
    }
}
</script>

<style lang="less" scoped>
.dialog-content {
    max-height: 70vh;
    overflow-y: auto;
    overflow-x: hidden;
    box-sizing: border-box;
}

</style>
